import json
from vocode import *
from vofactory import *
import time
import os
import glob
import remote_message

def remove_old_files(path, ext):
    r = glob.glob(path + ext)
    for f in r:
        os.remove(f)

def vomake(config):
    as3_output_path = '../../../client/resource/config/com/showgood/vo/'
    java_output_path = '../../src/java/com/showgood/vo/'

    remove_old_files(as3_output_path, '*.as')
    remove_old_files(java_output_path, '*.java')

    # Vo
    vid = 1    
    for cls in config:
        cls['vid'] = vid
        vid += 1
        
        as3 = As3Maker(cls, as3_output_path)
        as3.go()

        java = JavaMaker(cls, java_output_path)
        java.go()
        
    # VoFactory
    t = time.localtime()
    vn=int('{0}{1}'.format(t[0]%100,''.join(map(lambda x: str(x) if x >= 10 else '0'+str(x),t[1:5]))))
    JavaFactory(config, java_output_path).go(vn)
    As3Factory(config, as3_output_path).go(vn)
    
def main(file):
    # load config
    fp = open(file)
    config = json.load(fp)
    fp.close()
    # print(config)
    # make vo
    vomake(config)

    remote_message.main()

if __name__ == '__main__':
    import sys
    if len(sys.argv) >= 2:
        main(sys.argv[1])
    else:
        main('showgood.json')
    
